"""
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""

import cv2
import os
from pathlib import Path
from PyQt5.QtCore import Qt, QThread, pyqtSignal

class translationFPS2image(QThread):
    imageReady = pyqtSignal()
    def __init__(self, videoPath, imagePath = None):
        super(translationFPS2image, self).__init__()
        self.videoPath = videoPath
        self.imagePath = imagePath
        self.VID_FORMATS = ['.mov', '.avi', '.mp4', '.mpg',  '.mpeg', '.m4v', '.wmv', '.mkv', '.mp3']

        self.videos2images()

    def videos2images(self):

        # 1.检测读取文件路径是否正确
        path_video = Path(self.videoPath)
        print("导入路径：", path_video)

        # 2. 生成存储文件夹
        if self.imagePath is None:
            save_name_dir = str(path_video).replace(str(Path(path_video.name)), "")
            self.imagePath = os.path.join(save_name_dir, str(Path(path_video.stem)))
            if not os.path.exists(self.imagePath):
                os.makedirs(self.imagePath)
        else:
            if not os.path.exists(self.imagePath):
                os.makedirs(self.imagePath)
        print("生成路径：", self.imagePath)

        # 3. 开始转换。打印正在处理文件的序号和他的文件名，并开始转换
        cap = cv2.VideoCapture(str(path_video))
        # 设置保存参数
        params = [cv2.IMWRITE_PNG_COMPRESSION, 0]
        frame_count = 0  # 给每一帧标号
        print("开始生成")
        while True:
            frame_count += 1
            flag, frame = cap.read()
            if not flag:  # 如果已经读取到最后一帧则退出
                break
            if os.path.exists(
                    self.imagePath + str(frame_count) + '.bmp'):  # 在源视频不变的情况下，如果已经创建，则跳过
                break
            rgb_frame = cv2.cvtColor(frame, cv2.COLOR_RGB2RGBA)
            cv2.imwrite(self.imagePath + '\\' + str(frame_count) + '.bmp', rgb_frame)
            print(str(self.imagePath + '\\' + str(frame_count) + '.bmp'))
        cap.release()
        print("结束，并释放opencv资源")


if __name__ == '__main__':
    # 需要转换的视频路径列表，直达视频文件(自定义修改）
    video_path = r'F:\video\2023-05-20 17-40-55-frane3.avi'
    image_path = r'F:\video\2023-05-20 17-40-55-frame3-avi'
    # 进行转换
    translation = translationFPS2image(video_path, image_path)
